Source Code Viewer - Example 3 - UpdateCountryAction

package name.matthewgreet.example3.action;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.InterceptorRef;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;

import com.opensymphony.xwork2.ActionSupport;
import name.matthewgreet.example3.common.AbstractFormDrivenActionSupport;
import name.matthewgreet.example3.common.BrowserTabSession;
import name.matthewgreet.example3.common.ListCache;
import name.matthewgreet.example3.exception.CountryCodeAlreadyExistsRuleException;
import name.matthewgreet.example3.exception.CountryNotFoundRuleException;
import name.matthewgreet.example3.form.CountryForm;
import name.matthewgreet.example3.vo.CountryDTO;
import name.matthewgreet.example3.vo.CountryList;


/**
 * Updates country.  
 */
@InterceptorRef("FormDrivenStack")
@Results({
	@Result(name=ActionSupport.INPUT, type="redirectAction", params = {"namespace", "/", "actionName", "ViewCountry"}),
	@Result(name=ActionSupport.SUCCESS, type="redirectAction", params = {"namespace", "/", "actionName", "ViewCountry"})
})
public class UpdateCountryAction extends AbstractFormDrivenActionSupport<CountryForm> {
	private static final long serialVersionUID = -700627736127116372L;
	
	public UpdateCountryAction() {
		super();
	}

    @Action("/UpdateCountry")
    @Override
    public String execute() throws Exception {
    	CountryList countryList;
    	CountryDTO country, existingCountry;
    	BrowserTabSession session;
    	ListCache<Integer,CountryDTO> listCache;
    	
    	session = getBrowserTabSession();
    	countryList = CountryList.getInstance(session); 
    	listCache = ViewCountryListAction.retrieveListCache(session);
    	country = new CountryDTO(listCache.getSelected());
		country.setCode(getModel().getCode());
		country.setName(getModel().getName());
		country.setNotes(getModel().getNotes());
		try {
			countryList.updateCountry(country);
			listCache.setSelected(country);
			addActionMessage(country.getName() + " updated");
		}
		catch (CountryCodeAlreadyExistsRuleException e) {
			existingCountry = countryList.findById(e.getExistingId());
			addActionError("Country code " + e.getCode() + " already used by " + existingCountry.getName());
		}
		catch (CountryNotFoundRuleException e) {
			addActionError("Country recently deleted by someone else");
		}
		return ActionSupport.SUCCESS;
    }

	@Override
	protected CountryForm makeForm() {
		return new CountryForm();
	}

}